package com.example.test.config;


import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    public Docket createRestApi(){

        List<Parameter> list = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("Authorization").description("token 令牌， 需放在header中")
                .modelRef(new ModelRef("String")).parameterType("header")
                .required(false).build();
        list.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(list)
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.or(RequestHandlerSelectors.basePackage("com.example.test.agent.controller")
                        , RequestHandlerSelectors.basePackage("com.example.test.shiro")))
                .paths(PathSelectors.any())
                .build().directModelSubstitute(Timestamp.class, Date.class);

    }


    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("接口测试平台")
                .description("测试接口")
                .termsOfServiceUrl("localhost:8081/test_dome")
                .contact(new Contact("Swagger测试文档", "localhost:8081/test_dome/swagger-ui.html","00000"))
                .version("1.0")
                .build();
    }


}
